
const toastTypes = ['success', 'error', 'loading']

/**
 * 消息提示框
 * @param {string|Object} title 提示的内容，如果不是字符串类型，则为配置项
 * @param {Object} options 其他配置项
 * @property {function} success
 * @property {function} error
 * @property {function} loading
 * @return {Promise|undefined}
 */
const toast = (title, options = {}) => {
  if (typeof title === 'string') {
    options = {
      title,
      ...options
    }
  } else {
    options = {
      ...title,
      ...options
    }
  }

  return uni.showToast({
    icon: 'none',
    ...options
  })
}

toastTypes.forEach(type => {
  toast[type] = (title, options = {}) => {
    return toast(title, {
      ...options,
      icon: type
    })
  }
})

/**
 * loading 提示框
 * @param {string|Object} title 提示的内容，如果不是字符串类型，则为配置项
 * @param {Object} options 其他配置项
 * @return {Promise|undefined}
 */
const loading = (title, options = {}) => {
  if (typeof title === 'string') {
    options = {
      title,
      ...options
    }
  } else {
    options = title
  }

  return uni.showLoading({
    mask: true,
    ...options
  })
}

/**
 * 模态弹窗
 * @param {Object} options 配置项
 * @return {Promise|undefined}
 */
const modal = (options = {}) => {
  return uni.showModal({
    confirmColor: '#04a9a3',
    ...options
  })
}

/**
 * 从底部向上弹出操作菜单
 * @param {Object} options 配置项
 * @return {Promise|undefined}
 */
const actionSheet = (options) => {
  return uni.showActionSheet(options)
}

export default {
  toast,
  loading,
  modal,
  actionSheet,
}
